package app.netant.book.txt44;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Txt44Detail extends BaseDetail {

	public Txt44Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.44txt.com";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select("body .wrap .show .showBox .detail .detail_info .detail_right h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			
			int idx = title.indexOf("TXT");
			if(idx>0){
				title = title.substring(0,idx);
			}
			
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".rar");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select("body .wrap .show .showBox .detail .detail_info .detail_right ul li.small");
		for(int i=0;i<parts.size();i++){
			String tags = parts.get(i).text();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select("body .wrap .show .showBox.mt20 .showInfo");
		if(parts.size()>0){
			String memo = this.getString(parts.get(0).text());
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select("body .wrap .show .showBox .showDown ul li a.downButton");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("TXT下载")>=0){
				String href = parts.get(i).attr("href");
				if(href.startsWith("/")){
					href = this.preHref + href;
				}
				System.out.println("【Href】:"+href);
				
				int ed = href.lastIndexOf(".");
				if(ed>0){
					String exp = href.substring(ed);
					this.entity.setNovelFullName(this.entity.getNovelName()+exp);
				}
						
				href  = href.replaceAll("免费下载", "%E5%85%8D%E8%B4%B9%E4%B8%8B%E8%BD%BD");
				
				this.entity.setDownPath(href);
				break;
			}
		}
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select("body div.wrap.position span a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			System.out.println("【Type】："+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Txt44Detail detail = new Txt44Detail("http://www.44txt.com/book/136159199421541.html");
		detail.test();
	}



}
